package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.InspectionFileInfoDto;
import com.be.beadmin.quality.dto.input.InspectionInfoAllDto;
import com.be.beadmin.quality.dto.input.InspectionInfoAllEx;
import com.be.beadmin.quality.dto.input.InspectionTaskDto;
import com.be.beadmin.quality.dto.output.InspectionTaskEx;
import com.be.beadmin.quality.dto.output.InspectionTaskExcelEx;
import com.be.beadmin.quality.service.InspectionTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zjm
* @date 2023-05-28
**/
@Api(tags = "质量管理-检验单执行")
@RestController
public class InspectionTaskController {

    @Autowired
    private  InspectionTaskService inspectionTaskService;


    @PostMapping(value = "/quality/getInspectionTaskByPage")
    public ResultModel getInspectionTaskByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectionTaskService.getInspectionTaskByPage(pageEntity)) ;
    }

    @ApiOperation(value = "根据入厂检id获取检验单详细内容")
    @PostMapping("/quality/getInspectionTaskByProductQualityId")
    public ResultModel getInspectionTaskByProductQualityId(
            @ApiParam(value = "productQualityId",required = true)@RequestParam String productQualityId){
        try {
        return ResultModel.success(inspectionTaskService.getInspectionTaskByProductQualityId(productQualityId));
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation(value = "根据检验单Id获取检验单详细内容")
    @PostMapping("/quality/getInspectionTaskById")
    public ResultModel getInspectionTaskById(
            @ApiParam(value = "inspectionTaskId",required = true)@RequestParam String inspectionTaskId){
//        try {
            return ResultModel.success(inspectionTaskService.getInspectionTaskById(inspectionTaskId));
//        } catch (BeadminException e) {
//            return ResultModel.error(-1,e.getMessage());
//        } catch (Exception e) {
//            return ResultModel.error(-2,e.getMessage());
//        }
    }

    @ApiOperation(value = "根据工序Id获取检验单的值")
    @PostMapping("/quality/getInspectionTaskValueByProcessId")
    public ResultModel getInspectionTaskValueByProcessId(@RequestParam String processId){

        try {
            InspectionInfoAllEx inspectionTaskValueByProcessId = inspectionTaskService.getInspectionTaskValueByProcessId(processId);
            if (inspectionTaskValueByProcessId != null) {
                return ResultModel.success(inspectionTaskValueByProcessId);
            } else {
                return ResultModel.success("没有自检单");
            }
        } catch (Exception e) {
            return ResultModel.error(-1,e.getMessage());
        }
    }

    @ApiOperation(value = "提交检验单")
    @PostMapping("/quality/submitInspectionTask")
    public ResultModel submitInspectionTask(HttpServletRequest request, @RequestBody InspectionInfoAllDto inspectionInfoAllDto){
        inspectionTaskService.submitInspectionTask(request,inspectionInfoAllDto);
        return ResultModel.success();
    }

    @ApiOperation(value = "暂存检验单")
    @PostMapping("/quality/stagingInspectionTask")
    public ResultModel stagingInspectionTask(HttpServletRequest request, @RequestBody InspectionInfoAllDto inspectionInfoAllDto){
        inspectionTaskService.stagingInspectionTask(request,inspectionInfoAllDto);
        return ResultModel.success();
    }

    @ApiOperation(value = "完成检验单 -- 检验合格与否")
    @PostMapping("/quality/finishInspectionTask")
    public ResultModel finishInspectionTask(HttpServletRequest request,@RequestParam String id){ // 和
        inspectionTaskService.finishInspectionTask(request,id);
        return ResultModel.success();
    }

    @PostMapping("/quality/addInspectionTask")
    public ResultModel addInspectionTask(@RequestBody InspectionTaskDto inspectionTaskDto){
        inspectionTaskService.addInspectionTask(inspectionTaskDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delInspectionTask")
    public ResultModel delInspectionTask(@RequestBody InspectionTaskDto inspectionTaskDto){
        inspectionTaskService.delInspectionTask(inspectionTaskDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateInspectionTask")
    public ResultModel updateInspectionTask(@RequestBody InspectionTaskDto inspectionTaskDto){
        inspectionTaskService.updateInspectionTask(inspectionTaskDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportInspectionTask")
    public void exportInspectionTask(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectionTaskEx> inspectionTaskPage = inspectionTaskService.getInspectionTaskByPage(pageEntity);
        List<InspectionTaskEx> inspectionTaskExes = inspectionTaskPage.getContent();
        List<InspectionTaskExcelEx> inspectionTaskExcelExes=inspectionTaskExes.stream().map(item->{
            InspectionTaskExcelEx excel=new InspectionTaskExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectionTask接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectionTaskExcelEx.class).sheet("sheet1").doWrite(inspectionTaskExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectionTaskExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectionTaskExcelExes);
    }
}